package org.eclipse.mylyn.rememberthemilk.core;

import java.net.MalformedURLException;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.rememberthemilk.core.exceptions.RTMException;
import org.eclipse.mylyn.rememberthemilk.core.exceptions.RTMLoginException;
import org.eclipse.mylyn.rememberthemilk.core.exceptions.RTMPermissionDeniedException;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.osgi.framework.BundleContext;

public class RTMCorePlugin extends Plugin {

	
	
	// The plug-in ID
	public static final String PLUGIN_ID = "org.eclipse.mylyn.rememberthemilk.core";

	// The shared instance
	private static RTMCorePlugin plugin;
	
	public static final String ENCODING_UTF_8 = "UTF-8";

	public final static String REPOSITORY_KIND = "RememberTheMilk";

	private RTMRepositoryConnector connector;
	
	public RTMCorePlugin() {

	}

	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	public void stop(BundleContext context) throws Exception {
		if (connector != null) {
			connector.stop();
			connector = null;
		}
		plugin = null;
		super.stop(context);
	}
	
	public RTMRepositoryConnector getConnector() {
		return connector;
	}

	void setConnector(RTMRepositoryConnector connector) {
		this.connector = connector;
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static RTMCorePlugin getDefault() {
		return plugin;
	}
	
	/**
	 * Returns the path to the file caching repository attributes.
	 */
	protected IPath getRepostioryAttributeCachePath() {
		IPath stateLocation = Platform.getStateLocation(RTMCorePlugin.getDefault().getBundle());
		IPath cacheFile = stateLocation.append("repositoryConfigurations");
		return cacheFile;
	}
	
	public static IStatus toStatus(Throwable e, TaskRepository repository) {
		if (e instanceof RTMLoginException) {
			return RepositoryStatus.createLoginError(repository.getUrl(), PLUGIN_ID);
		} else if (e instanceof RTMPermissionDeniedException) {
			return RTMStatus.createPermissionDeniedError(repository.getUrl(), PLUGIN_ID);
		} else if (e instanceof RTMException) {
			String message = e.getMessage();
			if (message == null) {
				message = "I/O error has occured";
			}
			return new RepositoryStatus(repository.getUrl(), Status.ERROR, PLUGIN_ID, RepositoryStatus.ERROR_IO,
					message, e);
		} else if (e instanceof ClassCastException) {
			return new RepositoryStatus(Status.ERROR, PLUGIN_ID, RepositoryStatus.ERROR_IO,
					"Unexpected server response: " + e.getMessage(), e);
		} else if (e instanceof MalformedURLException) {
			return new RepositoryStatus(Status.ERROR, PLUGIN_ID, RepositoryStatus.ERROR_IO,
					"Repository URL is invalid", e);
		} else {
			return new RepositoryStatus(Status.ERROR, PLUGIN_ID, RepositoryStatus.ERROR_INTERNAL, "Unexpected error", e);
		}
	}

	/**
	 * Convenience method for logging statuses to the plug-in log
	 * 
	 * @param status
	 *            the status to log
	 */
	public static void log(IStatus status) {
		getDefault().getLog().log(status);
	}

	/**
	 * Convenience method for logging exceptions to the plug-in log
	 * 
	 * @param e
	 *            the exception to log
	 */
	public static void log(Throwable e) {
		String message = e.getMessage();
		if (e.getMessage() == null) {
			message = e.getClass().toString();
		}
		log(new Status(Status.ERROR, RTMCorePlugin.PLUGIN_ID, 0, message, e));
	}
	

}
